package com.test3;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;


public class Codes {
	
	public static void main(String[] args) throws IOException {
		String path = "E:/software/node.js/excel";
		
		URL url = new URL("https://wework.qpic.cn/wwpic/255720_N-7hksddRASmdSc_1682378073/0");		
		InputStream inputStream = new BufferedInputStream(url.openStream());
    	BufferedImage image = ImageIO.read(inputStream); 
		
		
		String realName =  "aa.png";
		String realPath = path+ "/" + "imgs" + "/" + Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date())) + "/" + realName;
		File diskFile = new File(realPath);
		if (!diskFile.getParentFile().exists()) {
			diskFile.getParentFile().mkdirs();
		}
		//weChatWrite(image, diskFile,6);

        ImageIO.write(card(), "png", diskFile);
		
	}

	public  static BufferedImage  card(){
        int width = 3400;
        int height = 2300;

        int  center = 3400/2;

        // 创建一个 BufferedImage 对象，宽度、高度和图像类型
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        // 获取 Graphics2D 上下文
        Graphics2D graphics = image.createGraphics();

        // 将图像背景设置为透明
       // graphics.setComposite(AlphaComposite.Clear);

        //graphics.fillRect(0, 0, width, height);

        // 在图像上绘制具有透明背景的内容
//        graphics.setComposite(AlphaComposite.Src);
//        graphics.setColor(Color.RED);
//        graphics.fillRect(0, 0, width, height);

        //长按识别文案
        graphics.setPaint(Color.BLACK);
        Font font = new Font("思源黑体 CN Medium",Font.BOLD,50);
        graphics.setFont(font);
        FontMetrics fontMetrics2 = graphics.getFontMetrics(font);
        graphics.drawString("王皮基", center +500, 600);

        graphics.drawString("满族自治县马鞍山镇北岗子村", center +500, 700);

        graphics.drawString("卫生旱厕", center +500, 800);

        graphics.drawString("2021", center +500, 900);

        BufferedImage codeImg = QRCodeUtil.createImage("UTF-8",
                "https://qcloud.s.weshaketv.com/1251097942/platform/cnag/h5_jl/online/index.html?uuid=6cbe5923cb4500b95555c5a3b392860f27934262",
                400,400);

//        URL url;
//        BufferedImage codeImg = null;
//        try {
//            url = new URL("https://qcloud.s.weshaketv.com/1251097942/platform/cnag/h5_jl/online/index.html?uuid=6cbe5923cb4500b95555c5a3b392860f27934262");
//            codeImg = ImageIO.read(url);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


        graphics.drawImage(codeImg, center + 700, 1580, 600, 600, null);//往画布添加图片，参数有位置及图片宽高
        graphics.dispose();
        return image;
    }



	
	/**
	 * 上传的图片写成插件码
	 * @param image 原本的code码
	 * @throws IOException 
	 * */
	public static void weChatWrite(BufferedImage image,File diskFile,final int days) throws IOException {
		String vname = "新民社区";
        //背景图
        URL url2 = new URL("http://1251097942.cdn.myqcloud.com/1251097942/platform/vns_imgs/jmzx/qiwei.png");
        BufferedImage image2 = ImageIO.read(url2);
        int createWidth = image2.getWidth();
        int createHeigh = image2.getHeight();
        BufferedImage back = new BufferedImage(createWidth, createHeigh, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = back.createGraphics();
        //绘制白色背景
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, createWidth,createHeigh);
        
        //消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //消除画图锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //添加插件码
        graphics.drawImage(image, 135, 370, 470, 460, null);//往画布添加图片，参数有位置及图片宽高

        //添加背景
        graphics.drawImage(image2, 0, 0, createWidth, createHeigh, null);//往画布添加图片，参数有位置及图片宽高
        
        
        
        //长按识别文案
        graphics.setPaint(Color.RED);
        Font font = new Font("思源黑体 CN Medium",Font.BOLD,40);

                
        //激活电子户码文案
        int nextHgigth = 63; //文字间隔高度
        int num = fontWrite(graphics,createWidth,235,"长按识别二维码 关注\""+vname+"\"并激活电子户码",17,nextHgigth,font);       
        Font font1 = new Font("思源黑体",Font.BOLD,48);
        graphics.setFont(font1);
//        FontMetrics fontMetrics1 = graphics.getFontMetrics(font1);
//        // 文字宽度
//        String text1 = "并激活电子户码";
//        int textWidth1 = fontMetrics1.stringWidth(text1);
//        // 计算出中心点，并且绘制出文字 x 居中
//        graphics.drawString(text1, createWidth/2 - textWidth1/2, 240+nextHgigth+(num*10));
        
        //失效文案
        graphics.setPaint(Color.GRAY);
        Font font3 = new Font("思源黑体",Font.PLAIN,28);    

        Calendar cl = Calendar.getInstance();
        cl.add(Calendar.DAY_OF_MONTH, +days);//+1今天的时间加一天
        int month =cl.get(Calendar.MONTH) + 1;   //获取月份，0表示1月份
        int day = cl.get(Calendar.DAY_OF_MONTH);//获取当前天数
        String msg = String.format("该二维码%d天内（%d月%d日前）有效重新获取将更新",days,month,day);
        fontWrite(graphics,createWidth,940,msg,17,58,font3);
        
        graphics.dispose();
        ImageIO.write(back, "png", diskFile);

	}
	
	/**
	 * @param graphics Graphics2D画布对象
	 * @param backWidth 背景宽
	 * @param y 文字从y轴什么地方写起
	 * @param msg 需要写入的文字
	 * @param length 按实际长度进行切割 写入
	 * @param nextHeigh 下一个高度
	 * @param font 字体
	 * @return 写入了几次
	 * */
	private static int fontWrite(Graphics2D graphics,int backWidth,int y,String msg,int length,int nextHeigh,Font font) {
		msg = StringUtils.isBlank(msg)?"":msg;
        
        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics(font);


        BigDecimal aBig = new BigDecimal(msg.length());
        BigDecimal bBig = new BigDecimal(length);
      //向上取整
        int num1 = aBig.divide(bBig,BigDecimal.ROUND_UP ).intValue();
        
        //根据行数决定高度
        y = num1==1?y:y-(num1*10);
        for(int i=0;i<num1;i++) {
        	String ms = msg.substring(i*length, (i+1)*length>msg.length()?msg.length():(i+1)*length);
            // 文字宽度
            int textWidth = fontMetrics.stringWidth(ms);
            graphics.drawString(ms, backWidth/2 - textWidth/2, y+i*nextHeigh);
        }
        return num1;
	}

}
